/*
 * librolgps, part of the RolGPS Proyect
 *
 *   
 * Copyright (C) Pablo Alejandro Costesich <rhfixer@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package deployers.sqlite;

import databaseHandler.Database;
import deployers.DatabaseDeployable;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author xeph
 */
public class Deployer extends Database implements DatabaseDeployable {

    Vector<Table> tables = new Vector();
    File database = null;

    public void addTable(Table table) {
        this.tables.add(table);
    }
    
    public void addTable(Table... tables) {
        for (Table table : tables){
            this.tables.add(table);
        }
    }

    public Deployer(File database) throws IOException {
            if (database.exists()) {
                if (!database.canRead() || !database.canWrite()) {
                    throw new IOException("Can't read or write to file");
                }
            }
        this.database = database;
        this.setDatabaseFile(database);
    }

    public void deploy() {
    // throw new UnsupportedOperationException("Not supported yet.");
    //Create the database file
            //append tables
        for(Table tabla : tables){
            try {
                this.executeStatement(tabla.sql());
                //System.out.println(tabla.sql());
            } catch (Exception ex) {
                Logger.getLogger(Deployer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }
    
    public String getDeploySQL() {
        StringBuffer sql = new StringBuffer();
        for(Table tabla : tables){
            try {
                sql.append(tabla.sql());
                sql.append("\n");
            } catch (Exception ex) {
                Logger.getLogger(Deployer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return sql.toString();
    }

    public void engine(String engine) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Table[] retrieveTableList() {
        //devuelve las tablas existentes en el deployer
        return new Table[1];
    }

    public void dropTableFromDeployer(Table table) {
        //borra una tabla del deployer
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
